package top.huhuiyu.springboot3.aop;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import top.huhuiyu.springboot3.exception.AppException;
import top.huhuiyu.springboot3.service.LimitService;

@Aspect
@Component
@Slf4j
@Order(20)
@RequiredArgsConstructor
public class LimitAop implements BaseAop {
    private final LimitService limitService;

    @After("controller()")
    public void after(JoinPoint jp) {
        log.debug("限流切面功能");
        MethodSignature ms = (MethodSignature) jp.getSignature();
        Limit limit = ms.getMethod().getAnnotation(Limit.class);
        log.debug("limit：{}", limit);
        if (limit == null) {
            return;
        }
        if (!limitService.checkLimit(limit)) {
            throw AppException.getInstance(limit.message());
        }

    }

}
